<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    append-to-body
    :close-on-click-modal="false"
    @close="closeDialog()"
    :visible="visible"
    width="600px"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="120px"
    >
      <el-form-item label="上级分类" prop="parentId">
        <treeselect v-model="dataForm.parentId"
                    :options="categoryOptions"
                    :normalizer="normalizer"
                    :show-count="true"
                    placeholder="选择上级菜单"
        />
      </el-form-item>

      <el-form-item label="分类名称" prop="categoryName">
        <el-input
          v-model="dataForm.categoryName"
          placeholder="分类名称"
        ></el-input>
      </el-form-item>

      <el-form-item label="排序" prop="categoryOrderBy">
        <el-input-number v-model="dataForm.categoryOrderBy" controls-position="right" :min="0"/>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit"
        >确定</el-button
      >
    </span>
  </el-dialog>
</template>

<script>
import { getObj, addObj, putObj, fetchTree } from "@/api/shop_goods/category";
import Treeselect from "@riophae/vue-treeselect"

export default {
  name:'GoodsCategoryEdit',
  props:{
    visible: {
      type: Boolean,
      default: false
    },
  },
  components: {Treeselect},
  data() {
    return {
      canSubmit: false,
      dataForm: {},
      dataRule: {
        parentId:[
          { required: true, message: "上级分类不能为空", trigger: "blur" },
        ],
        categoryName: [
          { required: true, message: "分类名称不能为空", trigger: "blur" },
        ],
        categoryOrderBy: [
          { required: true, message: "排序不能为空", trigger: "blur" },
        ]
      },
      // 菜单树选项
      categoryOptions: [],
    };
  },
  methods: {
    init(isEdit, id) {
      this.canSubmit = true;
      if (id !== null) {
        this.dataForm.parentId = id;
      }
      this.getTreeselect();
      if (isEdit) {
          getObj(id).then(response => {
            this.dataForm = response.data.data
          })
        } else {
          this.dataForm.id = undefined
        }
      
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.canSubmit = false;
          if (this.dataForm.parentId === undefined) {
            this.dataForm.parentId = 0
          }

          if (this.dataForm.id) {
            putObj(this.dataForm)
              .then((data) => {
                this.$message.success("修改成功");
                this.$emit("refreshDataList");
                this.closeDialog()
              })
              .catch(() => {
                this.canSubmit = true;
              });
          } else {
            addObj(this.dataForm)
              .then((data) => {
                this.$message.success("添加成功");
                this.$emit("refreshDataList");
                this.closeDialog()
              })
              .catch(() => {
                this.canSubmit = true;
              });
          }
        }
      });
    },
    //重置表单
    closeDialog() {
      this.$emit('update:visible', false)
    },

     /** 查询菜单下拉树结构 */
    getTreeselect() {
      fetchTree().then(response => {
        this.categoryOptions = []
        const dept = {id: 0, name: '根类型', children: response.data.data};
        this.categoryOptions.push(dept)
      });
    },

    /** 转换菜单数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children
      }

      return {
        id: node.id,
        label: node.name,
        children: node.children
      }
    }
  },
};
</script>
